package pump

import (
	"apidoc2entity/model"
	"fmt"
	_ "github.com/go-sql-driver/mysql"
	"github.com/go-xorm/xorm"
	"xorm.io/core"
)

type MySqlPump struct {
	x      *xorm.Engine
	DbName string
}

func NewPumpOfMysql() *MySqlPump {
	pump := &MySqlPump{}
	engine, err := xorm.NewEngine("mysql", "root:123456@tcp(localhost:30306)/information_schema?charset=utf8mb4")
	//设置日志显示
	engine.ShowSQL(true)
	engine.SetLogLevel(core.LOG_DEBUG)
	if err != nil {
		fmt.Printf("数据库连接失败:", err)
	}
	pump.x = engine
	return pump
}

func (pump *MySqlPump) PickerPump() []model.BuilderField {
	var res []model.BuilderField
	pump.x.Table("COLUMNS").
		Select("COLUMNS.TABLE_SCHEMA,COLUMNS.TABLE_NAME,COLUMNS.COLUMN_NAME,COLUMNS.DATA_TYPE,COLUMNS.CHARACTER_MAXIMUM_LENGTH,COLUMNS.COLUMN_COMMENT,TABLES.TABLE_COMMENT").
		Join("LEFT OUTER", "TABLES", "COLUMNS.TABLE_SCHEMA = TABLES.TABLE_SCHEMA And COLUMNS.TABLE_NAME=TABLES.TABLE_NAME").
		Where("COLUMNS.TABLE_SCHEMA =?", "union-busi").Find(&res)
	return res
	//out := make(chan *model.BuilderStruct)
	//go func() {
	//	defer close(out)
	//	for _,s := range sts {
	//		out<-&s
	//	}
	//}()
	//return out
}
